//
//  HWRequestLogManage
//
//  Created by 李含文 on 2021/4/28.
//  Copyright © 2021 MINISO. All rights reserved.
//

//DEBUG模式生效
#ifdef DEBUG
#import "HWPrintFormattingTools.h"
#import <objc/runtime.h>

static inline void HWLog_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    BOOL didAddMethod = class_addMethod(class,
                                        originalSelector,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

@implementation NSArray (HWLog)

//在load方法中完成方法交换
+ (void)load { //方法交换
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        HWLog_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(HWLog_descriptionWithLocale:indent:));
    });
}

- (NSString *)HWLog_descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int i = 0; i < level; i++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"[\n"];
    for (int i = 0; i < self.count; i++) {
         NSString *lastSymbol = (self.count == i + 1) ? @"":@",";
        id value = self[i];
        if ([value respondsToSelector:@selector(HWLog_descriptionWithLocale:indent:)]) {
            [mStr appendFormat:@"\t%@%@%@\n",tab,[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
        } else {
            [mStr appendFormat:@"\t%@%@%@\n",tab,value,lastSymbol];
        }
    }
    [mStr appendFormat:@"%@]",tab];
    return mStr;
}

@end

@implementation NSDictionary (HWLog)

//在load方法中完成方法交换
+ (void)load {  //方法交换
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        HWLog_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(HWLog_descriptionWithLocale:indent:));
    });
}

- (NSString *)HWLog_descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *mStr = [NSMutableString string];
    NSMutableString *tab = [NSMutableString stringWithString:@""];
    for (int i = 0; i < level; i++) {
        [tab appendString:@"\t"];
    }
    [mStr appendString:@"{\n"];
    NSArray *allKey = self.allKeys;
    for (int i = 0; i < allKey.count; i++) {
        id value = self[allKey[i]];
        NSString *lastSymbol = (self.count == i + 1) ? @"":@",";
        if ([value isKindOfClass:NSString.class]) { // 值是字符串
            if ([value respondsToSelector:@selector(HWLog_descriptionWithLocale:indent:)])
                [mStr appendFormat:@"\t%@\"%@\" : \"%@\"%@\n",tab,allKey[i],[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
            else
                [mStr appendFormat:@"\t%@\"%@\" : \"%@\"%@\n",tab,allKey[i],value,lastSymbol];
        } else { // 其他类型
            if ([value respondsToSelector:@selector(HWLog_descriptionWithLocale:indent:)])
                [mStr appendFormat:@"\t%@\"%@\" : %@%@\n",tab,allKey[i],[value descriptionWithLocale:locale indent:level + 1],lastSymbol];
            else
                [mStr appendFormat:@"\t%@\"%@\" : %@%@\n",tab,allKey[i],value,lastSymbol];
        }
    }
    [mStr appendFormat:@"%@}",tab];
    return mStr;
}

@end


#endif
